// 第一个图表--区域分布趋势

import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import { 
  RadarChart, 
  PolarGrid, 
  PolarAngleAxis, 
  PolarRadiusAxis, 
  Radar, 
  ResponsiveContainer, 
  Tooltip, 
  Legend 
} from 'recharts';

const PanelContainer = styled.div`
  height: 300px;
  background: rgba(10, 25, 41, 0.7);
  border: 1px solid rgba(0, 255, 255, 0.3);
  border-radius: 5px;
  padding: 15px;
  position: relative;
  overflow: hidden;
  
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, rgba(0, 255, 255, 0.8), transparent);
  }
  
  &:after {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.8));
  }
`;

const PanelHeader = styled.div`
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  position: relative;
  
  &:after {
    content: '';
    position: absolute;
    bottom: -6px;
    left: 0;
    width: 100%;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.5), transparent);
  }
`;

const PanelTitle = styled.div`
  font-size: 18px;
  font-weight: bold;
  color: #fff;
`;

const CustomTooltip = styled.div`
  background: rgba(10, 25, 41, 0.9);
  border: 1px solid rgba(0, 255, 255, 0.5);
  border-radius: 4px;
  padding: 8px;
  color: #fff;
  font-size: 12px;
`;

interface RadarChartPanelProps {
  title?: string;
}

const RadarChartPanel: React.FC<RadarChartPanelProps> = ({ title = "区域分布趋势" }) => {
  const [data, setData] = useState([
    { subject: '类别A', A: 120, B: 110, fullMark: 150 },
    { subject: '类别B', A: 98, B: 130, fullMark: 150 },
    { subject: '类别C', A: 86, B: 130, fullMark: 150 },
    { subject: '类别D', A: 99, B: 100, fullMark: 150 },
    { subject: '类别E', A: 85, B: 90, fullMark: 150 },
  ]);
  
  // 模拟数据更新
  useEffect(() => {
    const timer = setInterval(() => {
      setData(prevData => 
        prevData.map(item => ({
          ...item,
          A: Math.max(50, Math.min(150, item.A + Math.floor(Math.random() * 10) - 5)),
          B: Math.max(50, Math.min(150, item.B + Math.floor(Math.random() * 10) - 5))
        }))
      );
    }, 8000);
    
    return () => clearInterval(timer);
  }, []);
  
  const renderCustomTooltip = ({ active, payload }: any) => {
    if (active && payload && payload.length) {
      return (
        <CustomTooltip>
          <p>{`${payload[0].name}: ${payload[0].value}`}</p>
          {payload.length > 1 && <p>{`${payload[1].name}: ${payload[1].value}`}</p>}
        </CustomTooltip>
      );
    }
    return null;
  };

  return (
    <PanelContainer>
      <PanelHeader>
        <PanelTitle>{title}</PanelTitle>
      </PanelHeader>
      <ResponsiveContainer width="100%" height="90%">
        <RadarChart 
          outerRadius={85} 
          data={data} 
          margin={{ top: 5, right: 10, bottom: 25, left: 10 }}
        >
          <PolarGrid stroke="rgba(0, 255, 255, 0.3)" />
          <PolarAngleAxis 
            dataKey="subject" 
            tick={{ fill: '#fff', fontSize: 11 }}
            tickLine={{ stroke: 'rgba(0, 255, 255, 0.5)' }}
          />
          <PolarRadiusAxis 
            angle={30} 
            domain={[0, 150]} 
            tick={{ fill: '#fff', fontSize: 9 }}
            stroke="rgba(0, 255, 255, 0.3)"
            axisLine={{ stroke: 'rgba(0, 255, 255, 0.5)' }}
          />
          <Radar
            name="类别A"
            dataKey="A"
            stroke="#0088FE"
            fill="#0088FE"
            fillOpacity={0.6}
            dot={{ stroke: '#0088FE', strokeWidth: 2, fill: '#fff', r: 2.5 }}
            activeDot={{ r: 4, stroke: '#fff', strokeWidth: 2 }}
          />
          <Radar
            name="类别B"
            dataKey="B"
            stroke="#00C49F"
            fill="#00C49F"
            fillOpacity={0.6}
            dot={{ stroke: '#00C49F', strokeWidth: 2, fill: '#fff', r: 2.5 }}
            activeDot={{ r: 4, stroke: '#fff', strokeWidth: 2 }}
          />
          <Legend 
            wrapperStyle={{ color: '#fff', fontSize: '11px', bottom: 0 }}
            iconSize={8}
            layout="horizontal"
            verticalAlign="bottom"
            align="center"
          />
          <Tooltip content={renderCustomTooltip} />
        </RadarChart>
      </ResponsiveContainer>
    </PanelContainer>
  );
};

export default RadarChartPanel; 